package com.acmen.easyapi.parse;

import com.acmen.easyapi.model.ParamNode;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.acmen.easyapi.model.*;
import com.acmen.easyapi.util.StringUtil;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author acmen
 * 转成JSON数据
 */
@Component
public class DataParser {
    public JSON parseToData(ParamNode paramNode){
        JSONObject jsonObject = new JSONObject();
        List<ParamNode> subNodes = paramNode.getSubNodeList();
        if(subNodes==null||subNodes.isEmpty()){
            if(paramNode.isArray()){
                JSONArray array = new JSONArray();
                return array;
            }
            return null;
        }
        for(ParamNode node : subNodes){
            Object obj = parseToData(node);
            if(obj!=null){
                jsonObject.put(node.getName(),obj);
            }else if(StringUtil.isNotEmpty(node.getExample())){
                jsonObject.put(node.getName(),node.getExample());
            }else if(StringUtil.isNotEmpty(node.getExample2())){
                jsonObject.put(node.getName(),node.getExample2());
            }else{
               Object randomData = getRandomData(node.getType(),node.getLabel());
                jsonObject.put(node.getName(),randomData);
            }
        }
        if(paramNode.isArray()){
            JSONArray array = new JSONArray();
            array.add(jsonObject);
            return array;
        }else{
            return jsonObject;
        }
    }
    Random random = new Random();
    private Object getRandomData(String typeName,String desc) {
        if(typeName.endsWith("[]")){
            typeName = typeName.substring(0,typeName.length()-2);
            Object randomData = getRandomData(typeName,desc);
            if(randomData!=null){
                return Arrays.asList(randomData);
            }else{
                return null;
            }
        }
        if(Integer.class.getName().equals(typeName)||"int".equals(typeName)
            ||Short.class.getName().equals(typeName)||"short".equals(typeName)
            ||Long.class.getName().equals(typeName)||"long".equals(typeName)
                || BigDecimal.class.getName().equals(typeName)){
            return random.nextInt(100);
        }else if(Double.class.getName().equals(typeName)||"double".equals(typeName)
                ||Float.class.getName().equals(typeName)||"float".equals(typeName)){
            return random.nextDouble();
        }else if(Boolean.class.getName().equals(typeName)||"boolean".equals(typeName)){
            return random.nextBoolean();
        }else if(LocalDateTime.class.getName().equals(typeName)){
            return System.currentTimeMillis();
        }else if(String.class.getName().equals(typeName)){
            if(desc.endsWith("列表")){
                return desc.substring(0,desc.length()-2);
            }else{
                return desc;
            }
        }else{
            return null;
        }
    }
}
